package com.XinYun.Library.modules.Login.Servlet;

import com.XinYun.Library.modules.commons.ResponseWrapper.ResponseWrapper;
import com.XinYun.Library.utils.JWTUtils;
import com.alibaba.fastjson.JSON;
import com.XinYun.Library.modules.Login.Entity.Admin;
import com.XinYun.Library.modules.Login.Service.Impl.MemberServiceImpl;
import com.XinYun.Library.modules.Login.Service.MemberService;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileStore;
import java.util.HashMap;
import java.util.Map;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    MemberServiceImpl memberService = new MemberServiceImpl();
    Admin admin1 = new Admin();
    Admin admin2 = new Admin();
    Admin admin3 = new Admin();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BufferedReader reader = request.getReader();
        String readerLine = reader.readLine();
        JSONObject jsonObject = JSON.parseObject(readerLine);
        String sid = jsonObject.getString("Login_account");
        String password = jsonObject.getString("Login_password");

        HttpSession session = request.getSession();

        if ((sid.equals(admin1.getAdminSid1()) && password.equals(admin1.getAdminPwd1())) ||
                (sid.equals(admin2.getAdminSid2()) && password.equals(admin2.getAdminPwd2())) ||
                (sid.equals(admin3.getAdminSid3()) && password.equals(admin3.getAdminPwd3()))) {
            ResponseWrapper responseWrapper = null;
            //下发token
            String token = crateToken(sid);
            responseWrapper = ResponseWrapper.AdminSuccess(token);
            String s = JSON.toJSONString(responseWrapper);
            PrintWriter writer = response.getWriter();
            writer.println(s);
            System.out.println(s);
            //response.sendRedirect("SuccessLogin_Admin.jsp");
        } else if (memberService.isLogin(sid, password)) {
            ResponseWrapper responseWrapper = null;
            session.setAttribute("sid", sid);
            String token = crateToken(sid);
            responseWrapper = ResponseWrapper.UserSuccess(token);
            String s = JSON.toJSONString(responseWrapper);
            PrintWriter writer = response.getWriter();
            writer.println(s);
            System.out.println(s);
            //response.sendRedirect("SuccessLogin_User.jsp");
        } else {
            try {
                if (!memberService.isExist(sid) &&
                        (!sid.equals(admin1.getAdminSid1()) && !sid.equals(admin2.getAdminSid2()) && !sid.equals(admin3.getAdminSid3()))) {
                    ResponseWrapper responseWrapper = null;
                    responseWrapper = ResponseWrapper.AccountFail();
                    String s = JSON.toJSONString(responseWrapper);
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    System.out.println(s);
                    //request.getRequestDispatcher("Login.jsp").forward(request, response);
                } else if (!memberService.isLogin(sid, password) &&
                        (!password.equals(admin1.getAdminPwd1()) && !password.equals(admin2.getAdminPwd2()) && !password.equals(admin3.getAdminPwd3()))) {
                    ResponseWrapper responseWrapper = null;
                    responseWrapper = ResponseWrapper.PasswordFail();
                    String s = JSON.toJSONString(responseWrapper);
                    PrintWriter writer = response.getWriter();
                    writer.println(s);
                    System.out.println(s);
                    //request.getRequestDispatcher("Login.jsp").forward(request, response);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }

    /**
     * 生成令牌
     * @return
     */
    private String crateToken(String sid){
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("sid",sid);
        return JWTUtils.generateJwt(dataMap);
    }
}
